package com.fuyao.cloud.admin.flow.listener;

import com.fuyao.cloud.admin.flow.service.flowable.IDynamicCallbackService;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.impl.el.FixedValue;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * flowable 任务动态配置监听器。
 */
@Slf4j
@Getter
@Setter
@Component(value = "taskBusinessCallListener")
public class TaskBusinessCallListener implements TaskListener {
    /**
     * nacos的服务id
     */
    private FixedValue serviceId;
    /**
     * 访问的url路径
     */
    private FixedValue url;
    /**
     * 参数 多个的话用分号隔开 实例 userCode:00004737;status:1
     */
    private FixedValue params;

    @Resource
    private IDynamicCallbackService dynamicCallbackService;


    @Override
    public void notify(DelegateTask delegateTask) {
        String processInstanceId = delegateTask.getProcessInstanceId();
        String businessKey = delegateTask.getTaskDefinitionKey();
        String serviceIdStr = null, urlStr = null, paramsStr = null;
        if (serviceId != null) {
            serviceIdStr = serviceId.getExpressionText();
        }
        if (url != null) {
            urlStr = url.getExpressionText();
        }
        if (params != null) {
            paramsStr = params.getExpressionText();
        }
        Map<String, String> params = new HashMap<>();
        params.put("processInstanceId", processInstanceId);
        params.put("params", paramsStr);
        params.put("businessKey", businessKey);

        dynamicCallbackService.postCallback(serviceIdStr, urlStr, params);
    }
}

